package StudentSysterm;

import com.sun.prism.shader.Texture_ImagePattern_AlphaTest_Loader;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class StudentTest {
    public static void main(String[] args) {
        ArrayList<Student> list=new ArrayList<>();
        Scanner sc=new Scanner(System.in);

        loop:while (true) {
            System.out.println("欢迎来到学生管理系统");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            System.out.println("3.查询学生");
            System.out.println("4.修改学生");
            System.out.println("5.退出");
            System.out.println("--------------------------------------");
            String choose = sc.next();
            switch(choose){
                case "1":{
                    addStudent(list);
                    break;
                }
                case "2":{
                    deleteStudent(list);
                    break;
                }
                case "3":{
                    queryStudent(list);
                    break;
                }
                case "4":{
                    moveStudent(list);
                    break;
                }
                case "5":{
                    System.out.println("退出");
                    break loop;
                }

            }
        }

    }

    public static void addStudent(ArrayList<Student> list){
        Scanner sc=new Scanner(System.in);
        //添加学生对象
        Student student =new Student();
        //先输入学号进行判断
        while (true) {
            System.out.println("请输入学号");
            String id = sc.next();
            boolean flag = idcontins(list, id);
            //判断学号唯一性
            if(flag){
                System.out.println("学号已重复，请重新输入学号");
            }
            else{
                student.setId(id);
                break;
            }
        }
        //输入学生信息
        System.out.println("请输入姓名");
        String name = sc.next();
        student.setName(name);
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        student.setAge(age);
        System.out.println("请输入地址");
        String address = sc.next();
        student.setAddress(address);
        //把对象添加到集合
        list.add(student);
        //提示成功
        System.out.println("添加成功");
    }

    public static void deleteStudent(ArrayList<Student> list){
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入要删除的学生的学号");
        String id = sc.next();
        int indexcheck = indexcheck(list, id);
        if(indexcheck<0){
            System.out.println("没有此学生信息，请检查");
            return;
        }
        list.remove(indexcheck);
        System.out.println("删除成功");
    }

    public static void queryStudent(ArrayList<Student> list){
        //遍历学生信息
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            System.out.println(student.getId()+"\t"+student.getName()+"\t"+
                    student.getAge()+"\t"+student.getAddress());
        }
    }

    public static void moveStudent(ArrayList<Student> list){
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入要修改的学生的学号");
        String id = sc.next();
        int indexcheck = indexcheck(list, id);
        if(indexcheck<0){
            System.out.println("没有此学生信息，请检查");
            return;
        }
        //输入学生信息
        Student student=list.get(indexcheck);
        System.out.println("请输入姓名");
        String name = sc.next();
        student.setName(name);
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        student.setAge(age);
        System.out.println("请输入地址");
        String address = sc.next();
        student.setAddress(address);
        System.out.println("修改成功");
    }
    //判断学号唯一性
    public static boolean idcontins(ArrayList<Student> list ,String id){
        for (int i = 0; i < list.size(); i++) {
            String sid = list.get(i).getId();
            if(sid.equals(id)){
                return true;
            }
        }
        return false;

    }

    //判断学号的位置
    public  static  int indexcheck(ArrayList<Student> list ,String id){
        for (int i = 0; i < list.size(); i++) {
            String sid = list.get(i).getId();
            if(sid.equals(id)){
                return i;
            }
        }
        return -1;
    }
}
